﻿
Public Class frmRead

    Private mStations As New LvlStations
    Private mCompare As New LvlCompare
    Private mLastRoutePointName As String = String.Empty
    Private mBackColor As Color, mForeColor As Color

    'Public Shared ProjectName As String



    '记录TreeView1回车后焦点是否转移至下一Node,默认为False
    Private TreeView1MoveToNextNode As Boolean

    ''' <summary>
    ''' 开始观测前，完成配置
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadPrepare()


    End Sub
    Private Sub LoadPrepare()


        '读入设置参数
        'Dim mDb As New Db_CurrentOption
        MyShared.CurrentOpt = MyShared.myDb.CurrentRead

        'Dim mDb1 As New Db_Project
        MyShared.myDb.ProjectRead(MyShared.CurrentOpt.CurrentProject, MyShared.ProjectOpt)

        'Dim mDb2 As New Db_XianCha
        MyShared.myDb.XianChaRead(MyShared.ProjectOpt.XC_Grade, MyShared.ReadXC)

        ' 设置工程与文件
        mBackColor = txtName.BackColor
        mForeColor = txtName.ForeColor
        lStatus.BackColor = Color.Gray
        lStatus.ForeColor = Color.Blue

        ReadCompareFile()
        ReadThisFile()
        UpdateTreeViewShow()

        '打开输入法,设置输入界面
        Me.InputPanel1.Enabled = True
        SetInputReadShow()
    End Sub
  
    Private Sub ReadCompareFile()
        Dim mStns As New LvlStations
        Dim fn As String = MyShared.CurrentOpt.CompareFile
        Dim sTmp As String = ReadFile(fn).Trim
        If sTmp.Length > 0 Then mStns.Read(sTmp)

        'mStations.PointNameCopyFrom(mStns)
        Dim pntnames() As String = mStns.PointNames.Split(vbCrLf)

        SetPntsName(pntnames)
        If lstPntNames.Items.Count < 3 Then
            lstPntNames.Items.Add("BM")
            lstPntNames.Items.Add("TP")
            lstPntNames.Items.Add("M")
            lstPntNames.Items.Add("Q")
            lstPntNames.Items.Add("S")
            lstPntNames.Items.Add("W")
            lstPntNames.Items.Add("L")
            lstPntNames.Items.Add("Y")
        End If
        mCompare = New LvlCompare
        mStns.SetCompare(mCompare)
    End Sub
    Private Sub SetPntsName(ByRef Pnts() As String)
        lstPntNames.Items.Clear()
        For Each ln In Pnts
            If ln.Trim = Pnts(Pnts.Length - 1) Then
            Else
                If ln.Trim.Length > 0 Then
                    lstPntNames.Items.Add(ln.Trim)
                End If
            End If
        Next
    End Sub

    Private Sub ReadThisFile()
        Dim fn As String = MyShared.CurrentOpt.RecordFile
        Dim sTmp As String = ReadFile(fn).Trim
        If sTmp.Length > 0 Then
            'mStations = New LvlStations
            '读取时会把对象先清空,后赋值
            mStations.Read(sTmp)
        End If
        UpdateTreeViewShow()
    End Sub
    Private Function ReadFile(ByVal FileName As String) As String
        Dim prj As String = MyShared.CurrentOpt.CurrentProject
        Dim sTmp As String = String.Empty
        Try
            If prj.Length > 0 And FileName.Length > 0 Then
                Dim pf As New PathAndFile(prj)
                PathAndFile.ReadFile(prj, FileName, sTmp)
            End If
            Return sTmp
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function

    ''' <summary>
    ''' 退出前保存数据
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        SaveToFile()
    End Sub


    ''' <summary>
    ''' 根据当前测站内容，更新TreeView的显示
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateTreeViewShow()

        Dim mStation As LvlStation = mStations.CurrentStation
        lblStnIndex.Text = mStations.StationIndexView

        If TreeView1.Nodes.Count = 0 Then SetBlankDisp()
        Dim BackNode As TreeNode = TreeView1.Nodes(0)

        BackNode.Text = mStation.BackPoint.DispString
        BackNode.Nodes.Clear()
        If mStation.MidPointCount > 0 Then
            For I As Integer = 0 To mStation.MidPointCount - 1
                Dim vP As String = mStation.MidPoint(I).DispString
                BackNode.Nodes.Add(vP)
            Next
        End If
        TreeView1.Nodes(1).Text = mStation.ForePoint.DispString
        BackNode.Expand()

        TreeView1.SelectedNode = BackNode
        SetThisPointDisplay()
    End Sub
    Private Sub SetBlankDisp()
        Dim BackNode As TreeNode = TreeView1.Nodes.Add("Rb OOOO")
        Dim ForeNode As TreeNode = TreeView1.Nodes.Add("Rf OOOO")
    End Sub

    ''' <summary>
    ''' 选中下一节点
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private Sub SelectNextNode()
        TreeView1MoveToNextNode = False

        If TreeView1.SelectedNode Is Nothing Then
            '没有选中作保节点，则选第一个节点
            TreeView1.SelectedNode = TreeView1.Nodes(0)
            Return
        ElseIf TreeView1.SelectedNode.Nodes.Count > 0 Then
            '选中的节点有子节点，则选第一个子节点
            Dim Node As TreeNode = TreeView1.SelectedNode.Nodes(0)
            TreeView1.SelectedNode = Node
            Return
        End If

        Dim ParentNode As TreeNode = TreeView1.SelectedNode.Parent
        If ParentNode Is Nothing Then
            '选中的节点没有父节点则为第一层
            Dim Index As Integer = TreeView1.SelectedNode.Index
            If Index < TreeView1.Nodes.Count - 1 Then
                '若选中的节点后还有节点，则选中的位置后移，否则不处理
                Dim Node As TreeNode = TreeView1.Nodes(Index + 1)
                TreeView1.SelectedNode = Node
            Else
                '为了能循环选中，再回到后视点
                Dim Node As TreeNode = TreeView1.Nodes(0)
                TreeView1.SelectedNode = Node
            End If

        Else
            Dim Index As Integer = TreeView1.SelectedNode.Index
            If Index < ParentNode.Nodes.Count - 1 Then
                '选中的节点有父节点,则为第二层
                '肯定为中视点，选判断有没有后续节点
                Dim Node As TreeNode = ParentNode.Nodes(Index + 1)
                TreeView1.SelectedNode = Node

            Else
                '没有后续节点则前视点选中
                Dim Node As TreeNode = TreeView1.Nodes(1)
                If Not Node Is Nothing Then
                    TreeView1.SelectedNode = Node
                End If

            End If
        End If

    End Sub


    ''' <summary>
    ''' 回车后焦点转移
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TreeView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TreeView1.KeyPress
        If e.KeyChar = Chr(13) Then CrLfPress(sender)
    End Sub

    ''' <summary>
    ''' 树节点中选取的观测点变换后，更新mThisPoint,更新显示
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

        lstChars1.Visible = False
        lstPntNames.Visible = False
        Dim mThisPoint As LvlSvPoint = CurrentPoint()

        If Not mThisPoint Is Nothing Then
            Select Case mThisPoint.Prop
                Case "Rb"
                    mStations.CurrentStation.BackPoint = mThisPoint
                Case "Rf"
                    mStations.CurrentStation.ForePoint = mThisPoint
                Case "Rz"
                    mStations.CurrentStation.AddMidPoint(mThisPoint)
                Case Else

            End Select
        End If

        SetThisPointDisplay()
        SetSelectedNodeBackColor()

    End Sub
    Private Sub SetThisPointDisplay()
        Dim mThisPoint As LvlSvPoint = CurrentPoint()
        If mThisPoint Is Nothing Then
        Else
            txtName.Text = mThisPoint.Name
            txtRead1.Text = StandardRead(mThisPoint.Read1)
            ExamIsNumeric(txtRead1)

            txtRead2.Text = StandardRead(mThisPoint.Read2)
            ExamIsNumeric(txtRead2)

            ExamTwiceRead(txtRead2)
        End If
    End Sub
    Private Function CurrentPoint() As LvlSvPoint
        Dim mThisPoint As LvlSvPoint
        If TreeView1.SelectedNode Is Nothing Then
            '没有选中的节点
            mThisPoint = Nothing
        ElseIf TreeView1.SelectedNode.Parent Is Nothing Then
            '当前选中的是第一层，对应后视或前视
            Dim Index As Integer = TreeView1.SelectedNode.Index
            If Index = 0 Then
                mThisPoint = mStations.CurrentStation.BackPoint
            ElseIf Index = 1 Then
                mThisPoint = mStations.CurrentStation.ForePoint
            Else
                mThisPoint = Nothing
                MsgBox("不可能的错误出现了！！")
            End If
        Else
            '当前选中的是第二层，对应中视点
            Dim Index As Integer = TreeView1.SelectedNode.Index
            mThisPoint = mStations.CurrentStation.MidPoint(Index)
        End If
        UpdateStatus(mThisPoint)
        Return mThisPoint
    End Function
    Private Sub SetSelectedNodeBackColor() 'BackColor表示选中状态
        For Each node As TreeNode In TreeView1.Nodes
            For Each childnode As TreeNode In node.Nodes
                childnode.BackColor = mBackColor
                'childnode.ForeColor = mForeColor
            Next
            node.BackColor = mBackColor
            'node.ForeColor = mForeColor
        Next
        TreeView1.SelectedNode.BackColor = Color.Blue
        'TreeView1.SelectedNode.ForeColor = Color.White
    End Sub


    ''' <summary>
    ''' 更新状态条
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateStatus(ByRef vT As LvlSvPoint)
        If vT Is Nothing Then Return
        If vT.ValidPoint Then
            Dim vP As LvlSvPoint = New LvlSvPoint
            With mStations.CurrentStation
                If .BackPoint.ValidPoint Then
                    vP = .BackPoint
                ElseIf .ForePoint.ValidPoint Then
                    vP = .ForePoint
                End If
            End With
            If vP.ValidPoint Then
                Dim mH As Double = mCompare.CompareHeight(vP.Name, vT.Name)
                If mH < 9999 Then
                    mH += vT.Height(vP)
                    lStatus.Text = vT.Name & " 与 " & vP.Name & " 高差较差" & Format(mH * 1000, "0.00mm")
                    If Math.Abs(mH) > 0.002 Then
                        lStatus.BackColor = Color.Red
                    ElseIf Math.Abs(mH) > 0.0005 Then
                        lStatus.BackColor = Color.Yellow
                    Else
                        lStatus.BackColor = Color.Gray
                    End If
                End If
            End If
        End If
    End Sub

    ''' <summary>
    ''' 三个列表控制件的高度调整洁
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub InputPanel1_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanel1.EnabledChanged
        SetInputReadShow()
    End Sub
    Private Sub SetInputReadShow()
        Dim bottom As Integer = IIf(InputPanel1.Enabled, Me.Height - InputPanel1.Bounds.Height, Me.Height) - lStatus.Height
        lStatus.Top = bottom
        TreeView1.Height = bottom - TreeView1.Top - 2
        lstPntNames.Height = bottom - bNextStation.Top
        lstChars1.Height = lstPntNames.Height

        '待选字母列表框显示设置
        lstChars1.Top = bNextStation.Top
        lstChars1.Left = TreeView1.Left
        lstChars1.Height = lStatus.Top - lstChars1.Top

        '待选点名列表框显示设置
        lstPntNames.Top = bNextStation.Top
        lstPntNames.Left = lstChars1.Right
        lstPntNames.Width = bNextStation.Right - lstPntNames.Left
        lstPntNames.Height = lStatus.Top - bNextStation.Top

    End Sub

#Region "点号编辑"


    ''' <summary>
    ''' 点号输入框获得焦点，打开点号与字母选取列表
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.GotFocus
        '打开输入面板
        InputPanel1.Enabled = True

        '显示字母点号切换按纽
        If lstPntNames.Visible = False Then
            SetInputReadShow()
            lstChars1.Visible = True
            lstPntNames.Visible = True
        End If

        Dim vP As LvlSvPoint = CurrentPoint()
        Select Case vP.Prop
            Case "Rb"
                '后视为空时推荐最后一个前视点编号
                If txtName.Text = "" Then txtName.Text = mLastRoutePointName
                chkForeNameAddAuto.Visible = False

            Case "Rf"
                '当前输入点是前视，且自动增加点号时，点号设置
                chkForeNameAddAuto.Visible = True
                If txtName.Text = "" And chkForeNameAddAuto.Checked Then ForeNameAutoAdd()

            Case "Rz"
                chkForeNameAddAuto.Visible = False

        End Select

    End Sub
    Private Sub chkForeNameAddAuto_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles chkForeNameAddAuto.CheckStateChanged
        If chkForeNameAddAuto.Checked Then
            ForeNameAutoAdd()
        End If
    End Sub
    Private Sub ForeNameAutoAdd()
        Dim bS As String, bN As Integer
        Dim bkName As String = mLastRoutePointName
        For I As Integer = Len(bkName) To 1 Step -1
            If IsNumeric(Mid(bkName, I, 1)) Then
            Else
                If I < Len(bkName) Then
                    bN = CInt(Mid(bkName, I + 1))
                Else
                    bN = 0
                End If

                bS = Mid(bkName, 1, I)
                txtName.Text = bS & (bN + 1).ToString
                Exit For
            End If
        Next
    End Sub

    Private Sub txtName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtName.KeyPress
        'lstPntNames.Visible = False
        If e.KeyChar = Chr(13) Then CrLfPress(sender)
    End Sub


    ''' <summary>
    ''' 点号更改后更改内存数据内的点号
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub txtName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
        Dim mThisPoint As LvlSvPoint = CurrentPoint()

        If TreeView1.SelectedNode Is Nothing Then
            MsgBox("请先选择观测点!")
            Return
        End If

        If txtName.Text.Length > 0 Then

            '更新点号数据
            mThisPoint.Name = txtName.Text
            TreeView1.SelectedNode.Text = mThisPoint.DispString

            '若为后视点，则更新最后的线路点
            If mThisPoint.Prop = "Rb" Then mLastRoutePointName = mThisPoint.Name

            '记录待选点名列表框显示的位置
            For I As Integer = 0 To lstPntNames.Items.Count - 1
                Dim sTmp As String = lstPntNames.Items(I)
                If txtName.Text.Trim.Length > 0 Then
                    If sTmp = txtName.Text Then lstPntNames.SelectedIndex = I
                    Exit For
                End If
            Next

        End If
    End Sub

    ''' <summary>
    ''' 选取点号后，关闭点号与字母列表框
    ''' 点字母后仍在点号编辑状态
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstPntNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPntNames.SelectedIndexChanged
        If lstPntNames.Visible Then
            txtName.Text = lstPntNames.SelectedItem
            lstChars1.Visible = False
            lstPntNames.Visible = False
            txtRead1.Focus()

        End If
    End Sub
    Private Sub lstChars_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles lstChars1.SelectedIndexChanged
        Dim lst As ListBox = CType(sender, ListBox)
        txtName.Text &= lst.SelectedItem
        txtName.SelectionStart = txtName.TextLength
        txtName.Focus()
    End Sub

#End Region

    ''' <summary>
    ''' 变换输入距离或基辅读数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        With Button4
            If .Text = "视距->" Then
                .Text = "基辅->"
            Else
                .Text = "视距->"
            End If
        End With
        TreeView1.Focus()
    End Sub

#Region "两次读数编辑"

    ''' <summary>
    ''' 基本分划、辅助分划读数只接受数字
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtRead1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles txtRead1.KeyPress, txtRead2.KeyPress

        Dim txt1 As TextBox = CType(sender, TextBox)
        NumericTextboxExam(txt1.Text, e)
        If e.KeyChar = Chr(13) Then CrLfPress(sender)
    End Sub
    Private Sub NumericTextboxExam(ByVal txtExist As String, ByRef e As System.Windows.Forms.KeyPressEventArgs)

        Select Case Asc(e.KeyChar)
            Case 32, 8, 48 To 57 '空格,退格,0-9不处理
            Case 46 '小数点，只能一个
                If InStr(txtExist, ".") <> 0 Then
                    e.Handled = True
                End If
            Case Else '其它的终止事件
                e.Handled = True
        End Select
    End Sub

    ''' <summary>
    ''' 基本分划读数框获取焦点，关闭点号编辑列表
    ''' 为空时检查辅助分划，辅助分划非空时计算应为读数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>

    Private Sub txtRead1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRead1.GotFocus
        lstChars1.Visible = False
        lstPntNames.Visible = False
        chkForeNameAddAuto.Visible = False

        If txtRead1.Text.Length = 0 Then
            '若与后视点同名，则提示后视读数
            Dim pName As String = txtName.Text
            With mStations.CurrentStation.BackPoint
                If .ValidPoint Then
                    If .Name = txtName.Text Then
                        Dim sFirst As String = "", sSecond As String = ""
                        JiFuCalculate(.Read1, "J"c, "J"c, sFirst, sSecond)
                        txtRead1.Text = sFirst
                        SetPropmt(sSecond, txtRead1.Top, txtRead1.Right)
                    End If
                End If
            End With
        End If

        '若为空，但Read2不为空，则计算301550之差
        If txtRead1.Text.Length = 0 Then
            Dim sTxt As String = txtRead2.Text.Replace(" ", "")
            If sTxt.Length > 0 Then

                Dim sFirst As String = "", sSecond As String = ""
                JiFuCalculate(sTxt, "F"c, "J"c, sFirst, sSecond)
                txtRead1.Text = sFirst
                SetPropmt(sSecond, txtRead1.Top, txtRead1.Right)

            Else
                Dim Name As String = txtName.Text.Trim
                If Name.Length > 0 Then
                    SetInstrCenterLevel()
                    Dim sR As String = String.Empty
                    If mCompare.GetRoundRead(Name, sR) Then
                        Dim sFirst As String = "", sSecond As String = ""

                        Dim DivedeBy As Integer = IIf(MyShared.ProjectOpt.MidReadUnit.ToString.Length = 6, 100, 1000)
                        sFirst = Math.Floor(sR / DivedeBy).ToString
                        sSecond = (sR - CDbl(sFirst) * DivedeBy).ToString(Mid(DivedeBy.ToString, 2))
                        txtRead1.Text = sFirst
                        SetPropmt(sSecond, txtRead1.Top, txtRead1.Right)

                    End If
                End If
            End If
        End If
        txtRead1.SelectionStart = txtRead1.Text.Length

    End Sub
    Private Sub JiFuCalculate(ByVal sRead As String, ByVal InJorF As Char, ByVal OutJorF As Char, ByRef sFirst As String, ByRef sSecond As String)
        Dim JF As Double = MyShared.ProjectOpt.JiFuCha
        Dim UNIT As Double = MyShared.ProjectOpt.MidReadUnit
        Dim cc As Double = JF / UNIT
        Dim sRd As Double = CDbl(sRead) + IIf(InJorF = "J"c, 0, -cc)
        sRd += IIf(OutJorF = "J"c, 0, cc)

        Dim DivedeBy As Integer = IIf(UNIT = 0.00001, 1000, 100)
        sFirst = Math.Floor(sRd / DivedeBy).ToString
        sSecond = (sRd - CDbl(sFirst) * DivedeBy).ToString(Mid(DivedeBy.ToString, 2))
    End Sub
    Private Sub SetPropmt(ByVal sTxt As String, ByVal iBottom As Integer, ByVal iRight As Integer)
        lPrompt.ForeColor = Color.Blue
        lPrompt.Text = sTxt
        lPrompt.Top = iBottom - lPrompt.Height
        lPrompt.Left = iRight - lPrompt.Width
        lPrompt.Visible = True
    End Sub
    Private Sub SetInstrCenterLevel()
        With mStations.CurrentStation
            If .BackPoint.ValidPoint Then
                mCompare.SetInstrCenterLevel(.BackPoint.Name, .BackPoint.AverageRead)
            ElseIf .ForePoint.ValidPoint Then
                mCompare.SetInstrCenterLevel(.ForePoint.Name, .ForePoint.AverageRead)
            Else
                For I As Integer = 0 To .MidPointCount - 1
                    Dim vP As LvlSvPoint = .MidPoint(I)
                    If vP.ValidPoint Then
                        mCompare.SetInstrCenterLevel(vP.Name, vP.AverageRead)
                        Exit For
                    End If
                Next
            End If
        End With
    End Sub


    ''' <summary>
    ''' 辅助分划读数框获取焦点，关闭点号编辑列表
    ''' 为空时检查基本分划，基本分划非空时计算应为读数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtRead2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRead2.GotFocus
        lstChars1.Visible = False
        lstPntNames.Visible = False
        chkForeNameAddAuto.Visible = False

        If txtRead2.Text.Length = 0 Then
            Dim sTxt As String = txtRead1.Text.Replace(" ", "")
            If sTxt.Length > 0 Then
                Dim sFirst As String = "", sSecond As String = ""
                JiFuCalculate(sTxt, "J"c, "F"c, sFirst, sSecond)
                txtRead2.Text = sFirst
                SetPropmt(sSecond, txtRead2.Top, txtRead2.Right)
            End If
        End If
        txtRead2.SelectionStart = txtRead1.Text.Length
    End Sub


    ''' <summary>
    ''' 读数输入完成后：
    '''     1）检查是否为数字，通过后在后三位前插入空格；
    '''     2）检查基辅读数是否超；
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtRead1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRead1.LostFocus
        Dim mThisPoint As LvlSvPoint = CurrentPoint()
        lPrompt.Visible = False
        Dim txt As TextBox = CType(sender, TextBox)
        If ExamIsNumeric(txt) Then
            If ExamTwiceRead(txt) Then
                '非空时保存数据
                '检查读数是否变换的过程由Point类做
                If txt.Text.Length > 0 Then
                    Dim mRead1 As String = txtRead1.Text.Replace(" ", "")
                    mThisPoint.Read1 = mRead1
                    TreeView1.SelectedNode.Text = mThisPoint.DispString
                End If
            End If
        End If
        AutoAddMidPoint()
        UpdateStatus(mThisPoint)
    End Sub
    Private Sub txtRead2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRead2.LostFocus
        Dim mThisPoint As LvlSvPoint = CurrentPoint()
        lPrompt.Visible = False
        Dim txt As TextBox = CType(sender, TextBox)
        If ExamIsNumeric(txt) Then
            If ExamTwiceRead(txt) Then
                '非空时保存数据
                '检查读数是否变换的过程由Point类做
                If txt.Text.Length > 0 Then
                    Dim mRead2 As String = txtRead2.Text.Replace(" ", "")
                    mThisPoint.Read2 = mRead2
                    TreeView1.SelectedNode.Text = mThisPoint.DispString
                End If
            End If
        End If
        AutoAddMidPoint()
        UpdateStatus(mThisPoint)
    End Sub
    '1）检查是否为数字；
    Private Function ExamIsNumeric(ByRef txtExam As TextBox) As Boolean
        Dim read1 As String = txtExam.Text.Replace(" ", "")
        If read1.Length = 0 Then Return True
        If IsNumeric(read1) Then
            txtExam.Text = StandardRead(read1)
            txtExam.BackColor = mBackColor
            TreeView1.SelectedNode.ForeColor = mForeColor
            Return True
        Else
            TreeView1.SelectedNode.ForeColor = Color.Red
            txtExam.BackColor = Color.Red
            Return False
        End If
    End Function
    Private Function StandardRead(ByVal sRead As String) As String
        Try
            '若读数为6位，空格后余三位；若为5位，则空格后余2位
            Dim iPos As Integer
            Select Case MyShared.ProjectOpt.MidReadUnit
                Case 0.00001
                    iPos = 3
                Case 0.0001
                    iPos = 2
                Case 0.001
                    iPos = 3
                Case Else
            End Select

            Return IIf(sRead.Length > iPos, sRead.Insert(sRead.Length - iPos, " "), sRead)
        Catch ex As Exception
            Return sRead
        End Try

    End Function

    '2）检查两次读数是否超限；
    Private Function ExamTwiceRead(ByRef txtExam As TextBox) As Boolean
        Dim sReada As String = txtRead1.Text.Replace(" ", "")
        Dim sReadb As String = txtRead2.Text.Replace(" ", "")
        If sReada.Length > 0 And sReadb.Length > 0 Then
            Dim ReadA As Single = CSng(sReada)
            Dim Readb As Single = CSng(sReadb)

            Dim JF As Double = MyShared.ProjectOpt.JiFuCha   'lvlRegistry.OptionDef.Read_JiFuDistance
            Dim UNIT As Double = MyShared.ProjectOpt.MidReadUnit  ' lvlRegistry.OptionDef.Read_Unit
            Dim XC As Double = MyShared.ReadXC.TwoReadsErr  'lvlRegistry.OptionDef.XC_TwiceRead

            If Math.Abs(ReadA - Readb + JF / UNIT) > XC / UNIT Then
                txtRead1.BackColor = Color.Red
                txtRead2.BackColor = Color.Red
                TreeView1.SelectedNode.ForeColor = Color.Red
                Return False
            End If
        End If

        txtRead1.BackColor = mBackColor
        txtRead2.BackColor = mBackColor
        TreeView1.SelectedNode.ForeColor = mForeColor
        Return True
    End Function

    ''' <summary>
    ''' 若最后一个中视点点号及读数之一为有效值，则自动增加一个空的中视点
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private Sub AutoAddMidPoint()
        Dim mCnt1 As Integer = mStations.CurrentStation.MidPointCount
        Dim vP As New LvlSvPoint
        vP.Prop = "Rz"
        mStations.CurrentStation.AddMidPoint(vP)
        Dim mCnt2 As Integer = mStations.CurrentStation.MidPointCount

        If mCnt2 > mCnt1 Then
            Dim BackNode As TreeNode = TreeView1.Nodes(0)
            Dim sTmp As String = vP.DispString
            BackNode.Nodes.Add(sTmp)
        End If
    End Sub

#End Region

#Region "测站变换"

    ''' <summary>
    ''' 编辑前一站
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub btnForeStation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bForeStation.Click

        mCompare.SetInstrCenterLevel()

        mStations.BeforeStation()
        ClearInputTextBox()
        UpdateTreeViewShow()
        TreeView1.Focus()


    End Sub

    ''' <summary>
    ''' 编辑下一站
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bNextStation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bNextStation.Click
        mCompare.SetInstrCenterLevel()
        SaveToFile()

        '保存前视点号
        If mStations.CurrentStation.ForePoint.ValidPoint Then
            mLastRoutePointName = mStations.CurrentStation.ForePoint.Name
        End If

        '获取下一测站的当前测站,并更新显示
        mStations.NextStation()
        ClearInputTextBox() '
        UpdateTreeViewShow()

        '若后视为空，则为新测站，后视点点号默认为前站的前视点点号
        If mStations.CurrentStation.BackPoint.Name = "" Then
            mStations.CurrentStation.BackPoint.Name = mLastRoutePointName
        End If
        TreeView1.Focus()
    End Sub
    Private Sub SaveToFile()
        Dim FileName As String
        Dim mProject As String = MyShared.CurrentOpt.CurrentProject
        Dim mSaveFile As String = MyShared.CurrentOpt.RecordFile
        Try
            If mProject.Length > 0 And mSaveFile.Length > 0 Then
                Dim pf As New PathAndFile(mProject)
                FileName = mSaveFile
            Else
                MsgBox("请设置工程名称及保存文件名！")
                Exit Sub
            End If
            Dim sTmp As String = mStations.ToString
            PathAndFile.SaveToFile(mProject, FileName, sTmp)
        Catch ex As Exception
            MsgBox("数据保存出错！")
        End Try


    End Sub
    Private Sub ClearInputTextBox()
        txtName.Text = ""
        txtRead1.Text = ""
        txtRead2.Text = ""
    End Sub

#End Region

    ''' <summary>
    ''' 设置回车输入后的焦点转移
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub CrLfPress(ByVal sender As System.Object)
        Dim ctr As Control = CType(sender, Control)
        Select Case ctr.Name
            Case "TreeView1"
                If TreeView1MoveToNextNode Then
                    SelectNextNode()
                    TreeView1MoveToNextNode = False
                Else
                    TreeView1MoveToNextNode = True
                    If txtName.TextLength = 0 Then
                        txtName.Focus()
                    ElseIf txtRead1.TextLength = 0 Then
                        txtRead1.Focus()
                    ElseIf txtRead2.TextLength = 0 Then
                        txtRead2.Focus()
                    Else
                        SelectNextNode()
                    End If
                End If
            Case "txtName"
                txtRead1.Focus()

            Case "txtRead1"
                If txtRead2.Text.Length = 0 Then
                    txtRead2.Focus()
                Else
                    TreeView1.Focus()
                End If

            Case "txtRead2"
                If txtRead2.TextLength > 0 Then
                    If txtRead1.Text.Length = 0 Then
                        txtRead1.Focus()
                        Exit Select
                    End If
                End If
                TreeView1.Focus()
                SelectNextNode()
            Case Else
                TreeView1.Focus()
        End Select
    End Sub


#Region "菜单命令"

    ''' <summary>
    ''' 查看测站数据
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuStn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuStn.Click
        DspToView()
        txtView.Text = mStations.ToString
        InputPanel1.Enabled = False
    End Sub
    Private Sub DspToView()
        If txtView.Visible = False Then
            txtView.Focus()
            SaveToFile() '先保存数据
            txtView.Width = Me.Width
            txtView.Height = Me.Height
            txtView.Location = New System.Drawing.Point(0, 0)
            txtView.Visible = True
        End If
    End Sub

    ''' <summary>
    ''' 返回观测
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuSv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSv.Click
        txtView.Visible = False
        InputPanel1.Enabled = True

        SetThisPointDisplay()
    End Sub

    ''' <summary>
    ''' 浏览计算资料
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCal.Click
        DspToView()
        lStatus.Text = "正在计算……"
        Dim mNet As New LvlNet(mStations)
        mNet.SearchNodes()              '找节点
        mNet.SearchLine()               '分配水准线路
        mNet.Adjust()                   '平差，计算节点高程
        mNet.Calculate()                '计算散点高程
        mNet.SearchBHC()                '搜索闭合差
        txtView.Text = mNet.ToString    '显示


        Dim FileName As String
        Dim mProject As String = MyShared.CurrentOpt.CurrentProject   'lvlRegistry.OptionDef.Project
        Dim mSaveFile As String = MyShared.CurrentOpt.RecordFile
        If mProject.Length > 0 And mSaveFile.Length > 0 Then
            Dim pf As New PathAndFile(mProject)
            FileName = mSaveFile
        Else
            MsgBox("请设置工程名称及保存文件名！")
            Exit Sub
        End If
        Dim sTmp As String = txtView.Text
        PathAndFile.SaveToPrintFile(mProject, FileName, sTmp)

        lStatus.Text = ""
        InputPanel1.Enabled = False
    End Sub

    ''' <summary>
    ''' 读入比对点
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MenuNames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim fl As New OpenFileDialog
        If fl.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim flName As String = fl.FileName
            Try
                Dim FN As System.IO.TextReader = New IO.StreamReader(flName)
                Dim sTmp As String = FN.ReadToEnd
                FN.Close()

                lstPntNames.Items.Clear()
                mCompare.ComparePoints.Clear()

                Dim pnts() As String = sTmp.Split(vbCrLf)
                For Each pnt As String In pnts
                    Dim vdata() As String = pnt.Trim.Split(",")

                    lstPntNames.Items.Add(vdata(0))
                    If IsNumeric(vdata(1)) Then
                        Dim vP As New LvlPoint(vdata(0), vdata(1))
                        mCompare.ComparePoints.Add(vP)
                    End If
                Next
            Catch ex As Exception
            End Try
        End If
    End Sub


#End Region

    
End Class